﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Utility;
using LotteryVote.Entity;

namespace LotteryVote.Logic.Member
{
    internal class ProxyStrategy : UserStrategy
    {
        private int[] species;
        private SystemRole role;

        public override void AddUser(Entity.User user)
        {
            if (user.UserInfo.ShareRate > 1)
                user.UserInfo.ShareRate = user.UserInfo.ShareRate.PercentageToDecimal(2);
            InitParams();
            var parent = DaUser.GetUserById(user.ParentId);
            CheckParentRole(parent, role - 1);
            user.RoleId = (int)role;
            user.UserInfo.AvailableGivenCredit = user.UserInfo.GivenCredit;
            CheckUserValid(user);
            CheckShareRate(parent, user);
            var betLimitList = DaBetLimit.ListLimitByUser(user.ParentId);
            var gameBetLimitList = DaGameBetLimit.ListLimitByUser(user.ParentId);
            DaUser.ExecuteWithTransaction(() =>
            {
                InsertUser(user);
                UpdateParentGivenCredit(parent, parent.UserInfo.AvailableGivenCredit - user.UserInfo.GivenCredit);
                InitUserCommission(user, species);
                InitUserBetLimit(user, betLimitList);
                InitUserGameBetLimit(user, gameBetLimitList);
            });
        }

        private void InitParams()
        {
            species = base.ParseParam<int[]>(UserStrategy.SPECIES);
            role = base.ParseParam<SystemRole>(UserStrategy.ROLE);
        }
    }
}
